package com.atmyc.yygh.hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atmyc.yygh.hospital.repository.DepartmentRepository;
import com.atmyc.yygh.hospital.service.DepartmentService;
import com.atmyc.yygh.model.hosp.Department;
import com.atmyc.yygh.vo.hosp.DepartmentQueryVo;

import com.atmyc.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    //上传科室接口
    @Override
    public void save(Map<String, Object> paramMap) {
        //paramMap转换为Department对象
        String paramMapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(paramMapString,Department.class);

        Department department1 =departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if(department1!=null){
            department1.setUpdateTime(new Date());
            department1.setIsDeleted(0);
            departmentRepository.save(department1);
        }else{
            department.setUpdateTime(new Date());
            department.setCreateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建Pageable对象
        Pageable pageable = PageRequest.of(page-1,limit);

        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department,matcher);
        Page<Department> all = departmentRepository.findAll(example,pageable);
        return all;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
        if(department!=null){
            departmentRepository.deleteById(department.getId());
        }
    }

    //根据医院编号查询医院信息
    @Override
    public List<DepartmentVo> findDepTree(String hoscode) {
        //创建list集合，用于最终封装
        List<DepartmentVo> result = new ArrayList<>();

        Department department =  new Department();
        department.setHoscode(hoscode);
        Example<Department> example= Example.of(department);
        List<Department> res = departmentRepository.findAll(example);
        Map<String, List<Department>> depMap = res.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历集合生成树形结构
        for(Map.Entry<String,List<Department>> entry:depMap.entrySet()){
            //大科室编号
            String bigcode = entry.getKey();
            //大科室对应的子科室
            List<Department> departmentList = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(departmentList.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for(Department dep:departmentList){
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(dep.getDepcode());
                departmentVo2.setDepname(dep.getDepname());
                //封装到list集合
                children.add(departmentVo2);
            }

            departmentVo1.setChildren(children);
            result.add(departmentVo1);
        }
        return result;
    }
 }
